package com.controller.front_controller;


import com.mypg.CollectChapter;
import com.mypg.WrongChapter;
import com.service.CollectService;
import com.service.UserQuestionService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/front_collect")
@Controller
public class FrontCollectController {

    @Autowired
    CollectService collectService;

    @Autowired
    UserService userService;

    @Autowired
    UserQuestionService userQuestionService;

    @RequestMapping("/collect_question.do")
    @ResponseBody
    public void doCollect_question(HttpSession session,int question_id,int flag){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        if(flag==1)
          collectService.collect(user_id,question_id);
        else
            collectService.cancelCollect(user_id,question_id);
    }

    @RequestMapping("/init_collect.do")
    @ResponseBody
    public int doInit_collect(HttpSession session,int question_id){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        int flag=collectService.selectCountByUser_idAndQuestion_id(user_id,question_id);
        return flag;
    }

    @RequestMapping("/init_collect_chapter.do")
    @ResponseBody
    public List<CollectChapter> doInit_collect_chapter(HttpSession session,String question_class){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        return collectService.selectCollectChapterByUser_id(user_id,question_class);
    }

    @RequestMapping("/init_wrong_chapter.do")
    @ResponseBody
    public List<WrongChapter> doInit_wrong_chapter(HttpSession session,String question_class){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        return userQuestionService.selectWrongChapterByUser_id(user_id,0,question_class);
    }

}
